/**
 * <p/>
 * 实现功能：业务类型
 * <p/>
 *
 * @author 秦有宝
 * @version 1.0.00
 */
EUI.BusinessTypeView = EUI.extend(EUI.CustomUI, {
    renderTo: null,
    initComponent: function () {
        this.gridCmp = EUI.GridPanel({
            renderTo: this.renderTo,
            border: false,
            //businessTypeText:"业务类型",
            title: this.lang.businessTypeText,
            tbar: this.initTbar(),
            gridCfg: this.initGrid()
        });
        this.addEvents();
    },
    initTbar: function () {
        var g = this;
        return [{
            xtype: "Button",
            iconCss: "ecmp-common-add",
            // addText: "新增",
            title: this.lang.addText,
            selected: true,
            handler: function () {
                g.operateWindow(false);
            }
        },'->', {
            xtype: "SearchBox",
            //searchText: "请输入代码或名称查询!",
            displayText: g.lang.searchText,
            onSearch: function (value) {
                g.gridCmp.localSearch(value);
            }
        }];
    },
    initGrid: function () {
        var g = this;
        return {
            loadonce: true,
            url: _ctxPath + "/businessType/listAll",
            colModel: [{
                label: this.lang.operateText, name: "operate", index: "operate", width: 80, align: "center",
                formatter: function (cellvalue, options, rowObject) {
                    if(rowObject.archiveType=="AUTOMATIC"){
                        return "<i class='ecmp-common-delete icon-space-left' title='" + g.lang.deleteText + "'></i>";
                    }
                    return "<i  class='ecmp-common-edit icon-space' title='" + g.lang.modifyText + "'></i><i class='ecmp-common-delete' title='" + g.lang.deleteText + "'></i>";
                }
            },
                { name: "id", index: "id", hidden: true},
                { name: "archiveType", index: "archiveType", hidden: true},
                { name: "tenantCode", index: "tenantCode", hidden: true},
                { name: "code", index: "code",label: g.lang.codeText, width: 100, sortable: true},
                { name: "name", index: "name",label: g.lang.nameText, width: 200, sortable: true},
                { name: "archiveTypeRemark", index: "archiveTypeRemark", label: g.lang.archiveTypeRemarkText, sortable: true, width: 100, align: "center" },
                    // displayUrlText: "业务单据显示地址",
                { name: "displayUrl", index: "displayUrl", label: g.lang.displayUrlText, width: 300, align: "center" }
            ],
            sortname: "rank",
            shrinkToFit: false
        };
    },
    addEvents: function () {
        var g = this;
        $("#"+this.renderTo).on("click", ".ecmp-common-edit", function(e) {
            var data = g.gridCmp.getSelectRow();
            g.operateWindow(true);
            g.formCmp.loadData(data);
        } );
        $("#"+this.renderTo).on("click",".ecmp-common-delete", function () {
            var rowData = g.gridCmp.getSelectRow();
            var tipBox = EUI.MessageBox({
                //hintText: "温馨提示",
                title: g.lang.hintText,
                //deleteHintMessageText: "您确定要删除吗？",
                msg: g.lang.deleteHintMessageText,
                buttons: [{
                    // okText: "确定",
                    title: g.lang.okText,
                    selected: true,
                    handler: function () {
                        tipBox.remove();
                        var myMask = EUI.LoadMask({
                            // deleteMaskMessageText: "正在删除，请稍候...",
                            msg: g.lang.deleteMaskMessageText
                        });
                        EUI.Store({
                            url: _ctxPath + "/businessType/delete",
                            params: {
                                id: rowData.id
                            },
                            success: function (result) {
                                myMask.hide();
                                g.gridCmp.refreshGrid();
                                EUI.ProcessStatus(result);
                            },
                            failure: function (re) {
                                myMask.hide();
                                EUI.ProcessStatus(re);
                            }
                        });
                    }
                },{
                    //cancelText: "取消",
                    title: g.lang.cancelText,
                    handler: function () {
                        tipBox.remove();
                    }
                }]
            });
        });
    },
    operateWindow: function (isEdit) {
        var g = this;
        g.operateWin = EUI.Window({
            //addWinText: "新增{0}",
            //modifyWinText: "编辑{0}",
            //businessTypeText:"业务类型",
            title: isEdit ? String.format(g.lang.modifyWinText, g.lang.businessTypeText) : String.format(g.lang.addWinText, g.lang.businessTypeText),
            iconCss: isEdit ? "ecmp-eui-edit" : "ecmp-eui-add",
            height: 120,
            padding: 15,
            width: 380,
            items: [{
                xtype: "FormPanel",
                id: "editForm",
                padding: 0,
                defaultConfig: {
                    labelWidth: 90,
                    width: 270
                },
                items: [{
                    xtype: "TextField",
                    hidden: true,
                    name: "id"
                }, {
                    xtype: "TextField",
                    hidden: true,
                    name: "archiveType"
                }, {
                    xtype: "TextField",
                    hidden: true,
                    name: "tenantCode"
                },{
                    xtype: "TextField",
                    //codeText:"代码"
                    title: g.lang.codeText,
                    name: "code",
                    maxlength: 20,
                    allowBlank: false
                }, {
                    xtype: "TextField",
                    //nameText:"名称"
                    title: g.lang.nameText,
                    name: "name",
                    maxlength: 50,
                    allowBlank: false
                }]
            }],
            buttons: [{
                // saveText:保存
                title: g.lang.saveText,
                selected:true,
                handler: function () {
                    g.save();
                }
            },{
                //cancelText:取消
                title: g.lang.cancelText,
                handler: function () {
                    g.operateWin.remove();
                }
            }]
        });
        g.formCmp = EUI.getCmp("editForm");
    },
    save: function () {
        var g = this;
        if (!g.formCmp.isValid()) {
            // unFillText:"有必填项未输入，请确认！",
            EUI.ProcessStatus({msg: g.lang.unFillText, success: false});
            return;
        }
        var data = g.formCmp.getFormValue();
        if (!data.id) {
            delete data.id;
        }
        var myMask = EUI.LoadMask({
            //saveMaskMessageText: "正在保存，请稍候...",
            msg: g.lang.saveMaskMessageText
        });
        EUI.Store({
                url: _ctxPath + "/businessType/save",
                params: data,
                success: function (result) {
                    myMask.hide();
                    g.operateWin.remove();
                    EUI.ProcessStatus(result);
                    g.gridCmp.refreshGrid();
                },
                failure: function (re) {
                    myMask.hide();
                    g.operateWin.remove();
                    EUI.ProcessStatus(re);
                }
            }
        );
    }
});